package com.fhzn.auth.web.request;

import com.alibaba.fastjson.JSONObject;
import com.fhzn.auth.constant.Constants;
import com.fhzn.auth.entity.User;
import com.fhzn.auth.entity.UserRole;
import com.fhzn.auth.enums.CommonStatusEnum;
import com.fhzn.auth.tools.AESEncryptorUtils;
import com.fhzn.auth.tools.RandomUtil;
import com.fhzn.commons.toolkit.exception.BuzException;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/27
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UserOpRequest extends BaseRequest {

    private Integer id;

    private String nickname;

    private String nicknameCn;

    private Integer status;

    private Integer userType;

    private String mobile;

    private String tel;

    private String station;

    /**
     * 形如 xx:A; xxx:B
     */
    private String leaders;

    /**
     * 角色列表,逗号隔开
     */
    private String roles;

    /**
     * 角色 code 列表，逗号隔开
     */
    private String roleCodes;

    /**
     * 用户所属系统应用
     */
    private String organizationId;

    /**
     * 用户真实姓名
     */
    private String realname;

    private String email;

    /**
     * 硬件sn号
     */
    private String sn;

    public void checkAdd() {
        checkCommon();
    }

    public void checkUpdate() {
        if (id == null) {
            throw new BuzException("id不能为空");
        }
        checkCommon();
    }


    public void checkCommon() {
        this.checkBaseParams();
        if (status == null || !CommonStatusEnum.contains(status)) {
            throw new BuzException("状态位不正确");
        }

    }


    /**
     * 转换为服务层参数
     *
     * @return 用户信息
     */
    @Override
    public User genServiceParam() {
        String salt = RandomUtil.getRandomLetterAndNumberAndSymbol(AESEncryptorUtils.SALT_SIZE);
        User user = new User();
        user.setId(this.getId());
        user.setNickname(this.getNickname());
        user.setNicknameCn(this.getNicknameCn());
        user.setRealname(this.getRealname());
        user.setEmail(this.getEmail());
        user.setModifier(this.getHandler());
        user.setMobile(this.getMobile());
        user.setTel(this.getTel());
        user.setStatus(this.getStatus());
        user.setUserType(this.getUserType());
        user.setSn(this.getSn());
        JSONObject obj = new JSONObject();
        obj.put("loanStation", this.getStation());
        user.setUserExt(obj.toJSONString());
        user.setSalt(salt);
        // 默认密码超期
        user.setPasswordExpired(Constants.PASSWORD_EXPIRED);
        user.setPassword(AESEncryptorUtils.encrypt(RandomUtil.getRandomLetterAndNumberAndSymbol(8), salt));
        if (StringUtils.isNotBlank(this.getOrganizationId())) {
            user.setOrganizationId(Long.parseLong(this.getOrganizationId()));
        }
        return user;
    }

    public User addGenServiceParam() {
        User user = genServiceParam();
        user.setCreator(this.getHandler());
        return user;
    }

    public List<UserRole> genUserRoles() {
        if (StringUtils.isNotEmpty(roles)) {
            List<UserRole> userRoles = Lists.newArrayList();
            Arrays.stream(roles.split(","))
                    .forEach(r -> {
                        UserRole userRole = new UserRole();
                        userRole.setUserId(this.getId());
                        userRole.setRoleId(Integer.parseInt(r));
                        userRole.setModifier(this.getHandler());
                        userRole.setCreator(this.getHandler());
                        userRoles.add(userRole);
                    });
            return userRoles;
        }
        return Lists.newArrayList();
    }
}
